package user.web.servlet;


import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import seed.service.UserException;
import user.domain.User;
import user.service.UserService;

import cn.itcast.commons.CommonUtils;
import cn.itcast.servlet.BaseServlet;

public class UserServlet extends BaseServlet {
	UserService userService = new UserService();

	public String regist(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
        User form = CommonUtils.toBean(request.getParameterMap(), User.class);//封装表单数据到form中
        form.setUid(CommonUtils.uuid());
		/*
		 * 1.创建一个Map,用来装载错误信息，其中key为表单字段名称，值为错误信息
		 */
        Map<String,String> errors = new HashMap<String,String>();
        /*
         *2. 获取form中的username进行校验
         */
        String username = form.getUsername();
        if(username == null || username.trim().isEmpty())
        	errors.put("username", "用户名不能为空");
       /* if(username.length() > 2 || username.length() < 15)
        	errors.put("username", "用户名长度必须在2到15之间");*/
        /*
         *3. 判断是否存在错误信息
         */	
        if(errors.size() > 0){
            //保存错误信息
        	//保存表单数据
        	//转发到regist.jsp
        	request.setAttribute("errors", errors);
        	request.setAttribute("form", form);//为了回显
        	return "f:/regist.jsp";
    	}
        /*
         * 调用service的regist方法,会出来异常，try catch
         */
        try {
			userService.regist(form);
			/*
			 * userService执行成功
			 * 保存成功信息
			 * 转发到msg.jsp
			 */
			return "f:/login.jsp";
		} catch (UserException e) {
			//保存错误信息
        	//保存表单数据
        	//转发到regist.jsp
			request.setAttribute("msg", e.getMessage());
			request.setAttribute("form", form);//为了回显
			return "f:/regist.jsp";
		}

   }
	public String login(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException{
		/*
		 * 1.封装表单数据到form中
		 * 2.输入校验
		 * 3.调用service完成激活k
		 *    保存错误信息到request中
		 *    保存form到request中
		 *    转发到login.jsp中
		 * 4.保存用户信息到session中，重定向到index.jsp中   
		 */
		User form = CommonUtils.toBean(request.getParameterMap(), User.class);
		
		try {
			String admainuserName = "admin";
			String admainPassword = "123";
			if(form.getUsername().equals(admainuserName) && form.getPassword().equals(admainPassword)){
				User user = userService.login(form);
				request.getSession().setAttribute("session_user", user);
				return "r:/adminjsps/admin/index.jsp";
			}
		
			User user = userService.login(form);
			request.getSession().setAttribute("session_user", user);
			
			return "r:/index";
		} catch (UserException e) {
			request.setAttribute("msg", e.getMessage());
			request.setAttribute("form", form);//为了回显
			return "f:/login.jsp";
		}
	}
	public String message(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException{
		User form = CommonUtils.toBean(request.getParameterMap(), User.class);
		userService.message(form);
		request.setAttribute("user", "留言成功");
		return "f:/message.jsp";
	}
}
